{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d4e37e01-a168-4c51-8429-fed63de294e9",
   "metadata": {},
   "source": [
    "# 安装配置ChatTTS环境-推荐\n",
    "\n",
    "同济子豪兄 2024-6-11"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a67809cf-977a-4c2c-8dcf-7bd014f5395d",
   "metadata": {},
   "source": [
    "## 删除原有的ChatTTS目录"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4c329514-094e-454b-bb8d-34cc85243e71",
   "metadata": {},
   "outputs": [],
   "source": [
    "!rm -rf ChatTTS"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f89ec1bc-df36-49ff-b988-1e81f42b4337",
   "metadata": {},
   "source": [
    "## 从Github下载ChatTTS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7a4059af-3c89-4787-a4af-6fc02cb78382",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cloning into 'ChatTTS'...\n",
      "remote: Enumerating objects: 168, done.\u001b[K\n",
      "remote: Counting objects: 100% (130/130), done.\u001b[K\n",
      "remote: Compressing objects: 100% (81/81), done.\u001b[K\n",
      "remote: Total 168 (delta 92), reused 49 (delta 49), pack-reused 38\u001b[K\n",
      "Receiving objects: 100% (168/168), 2.73 MiB | 4.00 MiB/s, done.\n",
      "Resolving deltas: 100% (93/93), done.\n"
     ]
    }
   ],
   "source": [
    "!git clone https://github.com/2noise/ChatTTS.git"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c9de6da-da0f-4bd2-9952-2ea6d40d7d04",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 切换到子豪兄同个版本 20240613\n",
    "# !git reset --hard 0359f28545c7285d7ec3d98ca25c1099e7890c4a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d050e2a-9b15-40a2-b332-16084ad4e8e5",
   "metadata": {},
   "source": [
    "## 进入ChatTTS主目录"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "38fe6aa4-ce52-4597-933f-51e5bd7f0224",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "82c7f509-b017-4165-af64-c88ec928b37d",
   "metadata": {},
   "outputs": [],
   "source": [
    "os.chdir('ChatTTS')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "558425d7-75b9-4dd7-8254-7329f11012e9",
   "metadata": {},
   "source": [
    "## 安装所需工具包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "382fd845-5bf8-4820-9883-624a5c1ba6b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install -r requirements.txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b7437ab7-d64b-4b5e-a585-368fcf77404a",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install soundfile torchaudio pynini==2.1.5 nemo_text_processing gradio"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76101a3c-f610-42e3-a118-67d1057f8d07",
   "metadata": {},
   "source": [
    "## 验证安装成功"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5b39b649-896e-464a-a6d5-1dd064d24741",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torchaudio\n",
    "import torch\n",
    "import soundfile\n",
    "from IPython.display import Audio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c9958017-eb66-40d1-a3ef-379b46f702ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "from ChatTTS import ChatTTS\n",
    "# import ChatTTS"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da02030e-8aa4-4175-b058-b89983d697f1",
   "metadata": {},
   "source": [
    "如果导入上述工具包没有报错，则说明安装配置成功！"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b7d3e89-8b11-4620-8589-96decc311dac",
   "metadata": {},
   "source": [
    "## 修改代码\n",
    "\n",
    "将`ChatTTS/core.py`的143行到149行注释掉，效果如下：\n",
    "\n",
    "```python\n",
    "        # if do_text_normalization:\n",
    "        #     for i, t in enumerate(text):\n",
    "        #         _lang = detect_language(t) if lang is None else lang\n",
    "        #         self.init_normalizer(_lang)\n",
    "        #         text[i] = self.normalizer[_lang](t)\n",
    "        #         if _lang == 'zh':\n",
    "        #             text[i] = apply_half2full_map(text[i])\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dee9e371-687e-4c78-b3b6-b90a982c89d4",
   "metadata": {},
   "source": [
    "## 从Hugging Face下载预训练模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0e99f752-9d99-40b9-a9ed-e705ebb3dd26",
   "metadata": {},
   "outputs": [],
   "source": [
    "chat = ChatTTS.Chat()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "decf2454-369f-4c80-8705-2de8287a78ec",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:ChatTTS.ChatTTS.core:Download from HF: https://huggingface.co/2Noise/ChatTTS\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6736c1db6d7d4a8683d36894a34eea53",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Fetching 11 files:   0%|          | 0/11 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:ChatTTS.ChatTTS.core:use cuda:0\n",
      "INFO:ChatTTS.ChatTTS.core:vocos loaded.\n",
      "INFO:ChatTTS.ChatTTS.core:dvae loaded.\n",
      "INFO:ChatTTS.ChatTTS.core:gpt loaded.\n",
      "INFO:ChatTTS.ChatTTS.core:decoder loaded.\n",
      "INFO:ChatTTS.ChatTTS.core:tokenizer loaded.\n",
      "INFO:ChatTTS.ChatTTS.core:All initialized.\n"
     ]
    }
   ],
   "source": [
    "chat.load_models(force_redownload=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1672d9db-0fa3-492b-81c0-f06a1798d2db",
   "metadata": {},
   "source": [
    "如果无法下载，卡着不动，请移步至B2代码教程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "654b59f1-a36e-4ffe-b6e6-79ae5ea060fe",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
